package cn.maihe.elg.operation.service.system.impl;

import cn.maihe.elg.operation.repository.entity.OperateLogDO;
import cn.maihe.elg.operation.repository.entity.OperateLogDOCondition;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.system.OperateLogService;
import cn.maihe.elg.operation.service.system.UserService;
import cn.maihe.elg.operation.utils.DateUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import cn.maihe.framework.starter.mybatis.dao.MybatisDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description
 * @Author WJH
 * @Date 2021/03/22
 */
@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Resource
    private UserService userService;

    @Override
    public PageBo<OperateLogDO> selectPageList(int pageNo, int pageSize, String userCreate, String beginCreateTime, String endCreateTime) {
        OperateLogDOCondition builder = OperateLogDOCondition.builder();
        UserContext userContext = userService.getUserContext();

        if (!userContext.isSystemUser()) {
            builder.andOrgCodeEq(userContext.getOrgCode());
        }
        if (StringUtils.isNotBlank(userCreate)) {
            builder.andUserCreateLike("%" + userCreate + "%");
        }
        if (StringUtils.isNotBlank(beginCreateTime)) {
            builder.andGmtCreateGe(DateUtil.parseDate(beginCreateTime, "yyyy-MM-dd HH:mm:ss"));
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            builder.andGmtCreateLe(DateUtil.parseDate(endCreateTime, "yyyy-MM-dd HH:mm:ss"));
        }
        builder.orderDesc(OperateLogDO::getGmtCreate);

        return MybatisDaoImpl.run().selectPageBo(builder.build(), pageNo, pageSize);
    }
}
